; (function (window, $) {
  var dict = {
    sex: ['男', '女'],
    marryType: ['未婚', '已婚'],
    headship: ['机构负责人', '客户主管', '客户经理'],
    auditStatus: [
      '认证中',
      '认证成功',
      '<span style="color:#FF0000">认证失败</span>'
    ]
  }

  var computed = {
    birthday: function (timestamp) {
      var date = new Date(timestamp)
      return (
        date.getFullYear() +
        '年' +
        parseInt(date.getMonth() + 1) +
        '月' +
        date.getDate() +
        '日'
      )
    }
  }

  $(function () {
    $('.myprofile-example').on('click', function () {
      var diablock = $(this).data('name')
      var name = '.' + diablock
      $(name).css('display', 'block')
      $(name).on('click', function () {
        $(this).css('display', 'none')
      })
    })
    // login to see
    util.needChannelLogin(true)

    renderProfile()

    $('img.review').on('click', function () {
      util.imgReview($(this).attr('src'))
    })

    // just a datepicker
    $('input.datepicker').datepicker({
      language: 'zh-CN',
      format: 'yyyy-mm-dd'
    })

    // page change to see view ,aop a check function
    $('.step-btn').on('click', function () {
      if (
        parseInt($(this).data('thispage')) === 0 &&
        parseInt($(this).data('page')) === 1
      ) {
        if (!to1check()) {
          return
        }
      }

      if (
        parseInt($(this).data('thispage')) === 1 &&
        parseInt($(this).data('page')) === 2
      ) {
        if (!to2check()) {
          return
        }
      }

      step($(this).data('page'))
    })

    //when a file upload,get the oss url to hidden input

    $('input[type="file"]').on('change', function () {
      var fileInput = $(this)

      util.fileUpload(fileInput[0].files[0], function (res) {
        fileInput.prev('span').text('点击重新上传')
        fileInput.next('input[type="hidden"]').val(res.data)
      })
    })

    $('.upload-mypofile').hover(
      function () {
        var imgUrl = $(this)
          .find("input[type='hidden']")
          .val()
        console.log(imgUrl)
        if (imgUrl !== '') {
          $(this)
            .find('img')
            .attr('src', imgUrl)
          $(this)
            .find('a')
            .attr('href', imgUrl)
            .show()
        }
      },
      function () {
        $(this)
          .find('a')
          .hide()
      }
    )

    // input 200 
    $('input[name="profiles"]').on('keyup', function () {
      var len = $(this).val().length;
      if (len > 200) {
        layer.open({
          content: "自我介绍的内容长度不能超过200个字符",
          btn: "确定"
        })
        $(this).val($(this).val().substr(0, 199));
      }
    })

    //intro.js
    intro();

    // submit it will be a big function
    $('input.tijiao').on('click', function () {
      var formobj = {}

      if (!beforeSubmit()) {
        return
      }

      // get all text
      $('input[type="text"].formobj').each(function () {
        var key = $(this).attr('name')
        var val = $(this).val()
        formobj[key] = val
      })

      // get all select
      $('select.formobj').each(function () {
        var key = $(this).attr('name')
        var val = $(this).val()
        formobj[key] = val
      })

      // get all radio
      $('input[type="radio"].formobj:checked').each(function () {
        var key = $(this).attr('name')
        var val = $(this).val()
        formobj[key] = val
      })

      //get all hidden
      $('input[type="hidden"].formobj').each(function () {
        var key = $(this).attr('name')
        var val = $(this).val()
        formobj[key] = val
      })

      // some faker data

      // formobj.cityName = '南京'
      // formobj.provinceName = '江苏'

      // add isProfile

      formobj.isProfile = 1

      util.ajax({
        url: '/manager/profile/save',
        type: 'POST',
        data: formobj,
        success: function (res) {
          console.log(res)
          layer.alert('保存成功', {
            skin: 'demo-class',
            closeBtn: 0
          })
          // alert('保存成功')
          util.flushUserInfo(function () {
            window.location.reload()
          })
        }
      })
    })
  })

  function step(page) {
    switch (page) {
      case 0:
        $('.ownInfo-wrap').css('display', 'block')
        $('.jobInfo-wrap').css('display', 'none')
        $('.idInfo-wrap').css('display', 'none')
        $('.prof-step .step-2 i').css(
          'background',
          'url(.http://img0.rongsue.com/images/channel/logo.png) -200px -647px'
        )
        $('.prof-step .step-line2 i').css(
          'background',
          'url(.http://img0.rongsue.com/images/channel/logo.png) -200px -368px'
        )
        $('.prof-step .step-3 i').css(
          'background',
          'url(.http://img0.rongsue.com/images/channel/logo.png) -200px -766px'
        )
        break
      case 1:
        $('.ownInfo-wrap').css('display', 'none')
        $('.jobInfo-wrap').css('display', 'none')
        $('.idInfo-wrap').css('display', 'block')
        $('.prof-step .step-2 i').css(
          'background',
          'url(.http://img0.rongsue.com/images/channel/logo.png) -57px -647px'
        )
        $('.prof-step .step-line2 i').css(
          'background',
          'url(.http://img0.rongsue.com/images/channel/logo.png) -57px -368px'
        )
        $('.prof-step .step-3 i').css(
          'background',
          'url(.http://img0.rongsue.com/images/channel/logo.png) -200px -766px'
        )
        break
      case 2:
        $('.ownInfo-wrap').css('display', 'none')
        $('.idInfo-wrap').css('display', 'none')
        $('.jobInfo-wrap').css('display', 'block')
        $('.prof-step .step-3 i').css(
          'background',
          'url(.http://img0.rongsue.com/images/channel/logo.png) -57px -766px'
        )
        break
      default:
        break
    }
  }

  function renderProfile() {
    util.ajax({
      type: 'GET',
      url: '/user/manager',
      success: function (res) {
        var res = res.data
        util.null2empty(res)
        $('span.infobj').each(function () {
          var field = $(this).data('field')

          // check has dict attr?
          if ($(this).data('dict')) {
            $(this).text(dict[field][res[field]])
            return
          }

          // check has computed attr?
          if ($(this).data('computed')) {
            $(this).text(computed[field](res[field]))
            return
          }

          $(this).text(res[field])
        })

        $('img.infobj').each(function () {
          var field = $(this).data('field')
          $(this).attr(
            'src',
            res[field] === '' ? 'http://img0.rongsue.com/images/channel/av.png' : res[field]
          )
        })

        // $('span#useraddress').text(res.provinceName + res.cityName)

        // use isProfile to discuss what to show
        if (parseInt(res.isProfile)) {
          $('#hasprofile').show()

          $('#auditStatus')
            .html(dict.auditStatus[res.auditStatus])
            .css('color', '#20a920')
        } else {
          $('#noprofile').show()
        }

        //use auditstatus to discuss user can edit
        if (parseInt(res.auditStatus) === 0) {
          $('a.right-exit').on('click', function () {
            layer.alert('您的账户尚在认证中，暂时无法修改用户信息', {
              skin: 'demo-class',
              closeBtn: 0
            })
            // alert('您的账户尚在认证中，暂时无法修改用户信息')
          })
        } else {
          $('a.right-exit').on('click', function () {
            window.location.href = $(this).data('href')
          })
        }
      }
    })
  }

  // screen 0 to 1 check
  function to1check() {
    if ($("input[name='name']").val() === '') {
      $("input[name='name']").css('border-color', '#FF0000')
      return false
    }

    if ($("input[name='birthday']").val() === '') {
      $("input[name='birthday']").css('border-color', '#FF0000')
      return false
    }

    if ($("input[name='telPhone']").val() === '') {
      $("input[name='telPhone']").css('border-color', '#FF0000')
      return false
    }

    return true
  }

  //screen 1 to 2 check
  function to2check() {
    if ($("input[name='idCard']").val() === '') {
      $("input[name='idCard']").css('border-color', '#FF0000')
      return false
    }

    if ($("input[name='idCardFront']").val() === '') {
      layer.alert('证件照片正面未上传', {
        skin: 'demo-class',
        closeBtn: 0
      })
      // alert('证件照片正面未上传')
      return false
    }

    if ($("input[name='idCardVerso']").val() === '') {
      layer.alert('证件照片反面未上传', {
        skin: 'demo-class',
        closeBtn: 0
      })
      // alert('证件照片反面未上传')
      return false
    }

    if ($("input[name='idCardPhoto']").val() === '') {
      layer.alert('手持证件照片未上传', {
        skin: 'demo-class',
        closeBtn: 0
      })
      // alert('手持证件照片未上传')
      return false
    }

    return true
  }

  function beforeSubmit() {
    if ($("input[name='companyName']").val() === '') {
      $("input[name='companyName']").css('border-color', '#FF0000')
      return false
    }

    if (
      $("input[name='businessCard']").val() === '' &&
      $("input[name='workCard']").val() === '' &&
      $("input[name='contract']").val() === ''
    ) {
      layer.alert('名片 工牌 合同 至少选择一种上传', {
        skin: 'demo-class',
        closeBtn: 0
      })
      // alert('名片 工牌 合同 至少选择一种上传')
      return false
    }

    if ($("input[name='profiles']").val().length < 20) {
      layer.alert('自我介绍最少需要20字', {
        skin: 'demo-class',
        closeBtn: 0
      })
      $("input[name='profiles']").css('border-color', '#FF0000')
      return false
    }
    return true
  }

  function intro() {

    if (Cookies.get('myprofile_read')) {
      return;
    }

    introJs().setOptions({
      prevLabel: "上一步",
      nextLabel: "下一步",
      skipLabel: "跳过",
      doneLabel: "结束"
    }).oncomplete(function () {
      Cookies.set('myprofile_read', '1', { expires: 30 });
    }).onexit(function () {
      Cookies.set('myprofile_read', '1', { expires: 30 });
    }).start();
  }
})(window, jQuery)
